package com.base.controller;

import com.base.po.voip.SelfShopOrder;
import com.base.po.voip.Users;
import com.base.service.SelfShopOrderService;
import com.base.service.SelfShopWithdrawalsService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 自营商城收入明细
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopTransaction {
    @Autowired
    private SelfShopWithdrawalsService selfShopWithdrawalsService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private SelfShopOrderService selfShopOrderService;

    // 自营商城订单首页
    @RequestMapping("/selfShopTransaction")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        // 支付记录
        List<SelfShopOrder> selfShopOrders = null;
        //累计收益
        BigDecimal allCash = new BigDecimal(0);
        page.setCurrentPageNo(1);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(selfShopOrderService.countTransaction(adminUserId, adminUserId));
            selfShopOrders = selfShopOrderService.selectTransaction(page, adminUserId, adminUserId);
            allCash = selfShopOrderService.sumAllCash(adminUserId, adminUserId);
        }

        if (users.getRole() == 2) {
            page.setRecordTotal(selfShopOrderService.countTransaction(users.getParentid(), adminUserId));
            selfShopOrders = selfShopOrderService.selectTransaction(page, users.getParentid(), adminUserId);
            allCash = selfShopOrderService.sumAllCash(users.getParentid(), adminUserId);
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("allCash", allCash);
        request.setAttribute("selfShopOrders", selfShopOrders);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopTransaction");
        return modelAndView;
    }

    @RequestMapping("/selectSelfTransaction")
    public ModelAndView selectSelfTransaction(HttpServletRequest request, Integer curPage) {
        // 设置session缓存
        if (curPage == null) {
            if (request.getSession().getAttribute("curPage") != null) {
                curPage = (Integer) request.getSession().getAttribute("curPage");
            } else {
                curPage = 1;
            }
        }
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");

        ModelAndView modelAndView = new ModelAndView();
        Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

        //已有商品
        List<SelfShopOrder> selfShopOrders = null;
        //累计收益
        BigDecimal allCash = new BigDecimal(0);
        page.setCurrentPageNo(curPage);
        if (users.getRole() == 0 || users.getRole() == 1) {
            page.setRecordTotal(selfShopOrderService.countTransaction(adminUserId, adminUserId));
            selfShopOrders = selfShopOrderService.selectTransaction(page, adminUserId, adminUserId);
            allCash = selfShopOrderService.sumAllCash(adminUserId, adminUserId);
        }
        if (users.getRole() == 2) {
            page.setRecordTotal(selfShopOrderService.countTransaction(users.getParentid(), adminUserId));
            selfShopOrders = selfShopOrderService.selectTransaction(page, users.getParentid(), adminUserId);
            allCash = selfShopOrderService.sumAllCash(users.getParentid(), adminUserId);
        }

        request.setAttribute("totlenum", page.getRecordTotal()); // 分页
        request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

        request.setAttribute("allCash", allCash);
        request.setAttribute("selfShopOrders", selfShopOrders);
        request.setAttribute("adminUsergrade", adminUsergrade);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShopTransaction");
        return modelAndView;
    }
}
